package hui.zhang.service.take_delivery.impl;


import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hui.zhang.dao.take_delivery.WayBillRepository;
import hui.zhang.domain.take_delivery.WayBill;
import hui.zhang.index.dao.WayBillIndexRepository;
import hui.zhang.service.take_delivery.WayBillService;

@Service
@Transactional
public class WayBillServiceimpl implements WayBillService{

	@Autowired
	private WayBillRepository wayBillRepository;
	
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;
	
	@Override
	public Page<WayBill> findByPage(WayBill wayBill, Pageable pageable) {
		if(StringUtils.isBlank(wayBill.getWayBillNum())
			&& StringUtils.isBlank(wayBill.getSendAddress())
			&& StringUtils.isBlank(wayBill.getRecAddress())
			&& StringUtils.isBlank(wayBill.getSendProNum())
			&& (wayBill.getSignStatus()==null || wayBill.getSignStatus()==0)) {
			// 无条件查询
			return wayBillRepository.findAll(pageable);
		} else {
			// 查询条件
			//must 条件必须成立 and
			//must not 条件必须不成立not
			//should 条件可以不成立or
			BoolQueryBuilder query = new BoolQueryBuilder(); // 布尔查询，条件组合查询
			//向组合查询对象添加条件
			if(StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				// 运单号查询
				QueryBuilder tempQuery = new TermQueryBuilder("wayBillNum", 
						wayBill.getWayBillNum());
				query.must(tempQuery);
			}
			if(StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				// 发货地 模糊查询 情况一，词条存在 模糊
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("sendAddress",
						"*" + wayBill.getSendAddress() + "*");
				// 多词条 情况二，不存在
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(
						wayBill.getSendAddress()).field("sendAddress")
						.defaultOperator(Operator.AND);
				// 两种情况取or
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryStringQueryBuilder);
				
				query.must(boolQueryBuilder);
			}
			if(StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				// 速运类型 等值查询
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum", 
						wayBill.getSendProNum());
				query.must(termQuery);
			}
			if(wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				// 签收状态查询
				QueryBuilder termQuery = new TermQueryBuilder("signStatus", 
						wayBill.getSignStatus());
				
				query.must(termQuery);
			}
			
			SearchQuery searchQuery = new NativeSearchQuery(query);
			searchQuery.setPageable(pageable);
			return wayBillIndexRepository.search(searchQuery);
		}
	}

	@Override
	public void save(WayBill model) {
		// 判断运单号是否存在
		WayBill persistWayBill = wayBillRepository
				.findByWayBillNum(model.getWayBillNum());
		if(persistWayBill == null || persistWayBill.getId() == null) {
			
			model.setSignStatus(1); // 待发货
			
			wayBillRepository.save(model);
			
			// 保存同时搜索索引也保存一份
			wayBillIndexRepository.save(model);
		} else {
			// 存在
			try {
				if(persistWayBill.getSignStatus() == 1) {
					Integer id = persistWayBill.getId();
					BeanUtils.copyProperties(persistWayBill, model);
					persistWayBill.setId(id);
					persistWayBill.setSignStatus(1);
					// 保存索引
					wayBillIndexRepository.save(persistWayBill);
				} else {
					throw new RuntimeException("运单已经发出，无法修改！");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public WayBill findByWayBillNum(String wayBillNum) {
		return wayBillRepository.findByWayBillNum(wayBillNum);
	}

	// 同步索引库
	@Override
	public void syncIndex() {
		List<WayBill> wayBills = wayBillRepository.findAll();
		// 同步
		wayBillIndexRepository.save(wayBills);
	}

	@Override
	public List<WayBill> findWayBills(WayBill wayBill) {
		if(StringUtils.isBlank(wayBill.getWayBillNum())
			&& StringUtils.isBlank(wayBill.getSendAddress())
			&& StringUtils.isBlank(wayBill.getRecAddress())
			&& StringUtils.isBlank(wayBill.getSendProNum())
			&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {
			return wayBillRepository.findAll();
		} else {
			// 查询条件
			// must 条件必须成立 and
			// must not 条件必须不成立not
			// should 条件可以不成立or
			BoolQueryBuilder query = new BoolQueryBuilder(); // 布尔查询，条件组合查询
			// 向组合查询对象添加条件
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				// 运单号查询
				QueryBuilder tempQuery = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
				query.must(tempQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				// 发货地 模糊查询 情况一，词条存在 模糊
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("sendAddress",
						"*" + wayBill.getSendAddress() + "*");
				// 多词条 情况二，不存在
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getSendAddress())
						.field("sendAddress").defaultOperator(Operator.AND);
				// 两种情况取or
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryStringQueryBuilder);

				query.must(boolQueryBuilder);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				// 速运类型 等值查询
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());
				query.must(termQuery);
			}
			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				// 签收状态查询
				QueryBuilder termQuery = new TermQueryBuilder("signStatus", wayBill.getSignStatus());

				query.must(termQuery);
			}

			SearchQuery searchQuery = new NativeSearchQuery(query);
			Pageable pageable = new PageRequest(0, 10000);
			// 设置分页效果
			searchQuery.setPageable(pageable);
			
			return wayBillIndexRepository.search(searchQuery).getContent();
		}
	}

}
